package com.zhangjianling.premp.controller.sys;

import com.zhangjianling.premp.controller.base.BaseController;
import com.zhangjianling.premp.entity.sys.ResultEntity;
import com.zhangjianling.premp.entity.sys.SysDept;
import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.server.sys.SysDeptService;
import com.zhangjianling.premp.utils.DepartmentUtil;
import com.zhangjianling.premp.utils.SystemAopLog;
import com.zhangjianling.premp.utils.TimeHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;


/**
 * 系统部门管理
 *
 * @author zhangjl
 *
 */
@RestController
@RequestMapping("sysDept")
public class SysDeptController extends BaseController {
	@Autowired
	private SysDeptService sysDeptService;

	/**
	 * 分页部门列表
	 *
	 * @return
	 */
	@RequiresPermissions("sysDept:pageList")
	@PostMapping("pageList")
	public ResultEntity pageList(@RequestParam(value = "pageCurrent", defaultValue = "1") String pageCurrent, SysDept dept) {
		ResultEntity res = new ResultEntity();
		try {
			Page<SysDept> deptPage = sysDeptService.pageList(Integer.parseInt(pageCurrent), PAGESIZE, "createTime",dept);
			if (deptPage.getTotalPages() > 0) {
				res.setData(deptPage);
				res.setMessage(GET_SUCCESS);
				res.setCode(SUCCESS);
			} else {
				res.setMessage(IS_NULL);
				res.setCode(NULL);
			}
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}

	/**
	 * 保存部门
	 * @param dept
	 * @return
	 */
	@PostMapping("saveDept")
	@SystemAopLog(description="保存部门")
	public ResultEntity saveDept(SysDept dept,String parentDeptIds){
		ResultEntity res = new ResultEntity();
		Subject subject = SecurityUtils.getSubject();
		SysUser currentUser = (SysUser)subject.getPrincipal();
		String curUserName =  currentUser.getUserName();
		try {
			//设置部门
			if(StringUtils.isNotBlank(parentDeptIds)){
				SysDept pdept = new SysDept();
				pdept.setDeptId(parentDeptIds);
				dept.setParentDept(pdept);
			}
			if(StringUtils.isNotBlank(dept.getDeptId())){
				dept.setUpdateTime(TimeHelper.getCurrentTime());
				dept.setUpdateUser(curUserName);
			}else{
				dept.setCreateTime(TimeHelper.getCurrentTime());
				dept.setCreateUser(curUserName);
			}
			//保存部门
			sysDeptService.saveDept(dept);
			res.setMessage(OPERATE_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(OPERATE_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}


	/**
	 * 删除部门
	 * @return
	 */
	@PostMapping("delDept")
	public ResultEntity delDept(String ID){
		ResultEntity res = new ResultEntity();
		try {
			sysDeptService.deleteDept(ID);
			res.setMessage(DELETE_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(DELETE_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}

	/**
	 * 查询单个部门
	 * @return
	 */
	@GetMapping("findDept")
	public ResultEntity findDept(String ID){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(sysDeptService.findDept(ID));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}

	/**
	 * 查询部门
	 * @return
	 */
	@GetMapping("findTopList")
	public ResultEntity findTopList(){
		ResultEntity res = new ResultEntity();
		try {
			res.setData(DepartmentUtil.getAllDepartments(sysDeptService.findTopList()));
			res.setMessage(GET_SUCCESS);
			res.setCode(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}


	/**
	 * 查询部门编号是否存在
	 * @return
	 * @throws IOException
	 */
	@GetMapping("findDeptCode")
	public ResultEntity findDeptCode(String deptCode){
		ResultEntity res = new ResultEntity();
		try {
			SysDept sysDept = sysDeptService.findByDeptCode(deptCode);
			if(sysDept != null){
				res.setCode(SUCCESS);
				res.setMessage(GET_SUCCESS);
			}else{
				res.setMessage(IS_NULL);
				res.setCode(NULL);
			}

		} catch (Exception e) {
			e.printStackTrace();
			res.setMessage(GET_ERROR + ":" + e.getMessage());
			res.setCode(ERROR);
		}
		return res;
	}

}
